import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    rst: {
      brand: "品牌",
      shopName: "麦当劳麦乐送（广州华穗路店）",
      rank: 4.5,
      monthSale: 1169,
      fastTime: 28,
      deliver: "商家配送",
      deliverCost: 9,
      distance: 1269,
      foodBox: 1,
      sort: "id",
      currentDetailId: null,
      isOriginPrice: true,
      bannerUrl: "http://kxm.pub/shop/images/banner.jpg",
      logoUrl: "http://kxm.pub/shop/images/logo.jpg",
      hotSelect: [
        "双人早餐",
        "脆薯饼",
        "猪柳蛋",
        "板烧满分三件套",
        "菠萝鸡",
        "皮蛋鸡肉粥",
        "脆香油",
        "香芋派",
      ],
      notice:
        "雪人奇缘加“萌”麦乐送，新品多多，美味无限。安格斯牛堡系列菠萝来袭，雪人驾到小食限时尝鲜。",
      isPreferential: true,
      preIndex: 0,
      preferential: [
        { full: 30, subtract: 6 },
        { full: 60, subtract: 10 },
        { full: 100, subtract: 20 },
      ],
    },
    accounts: {
      total: null,
      count: null,
      number: null,
      preferential: null,
      tips: null,
    },
    filterData: [
      { id: 0, name: "综合排序", isClass: true },
      { id: 1, name: "好评优先", isClass: false },
      { id: 2, name: "销量优先", isClass: false },
    ],
    products: [
      {
        id: 0,
        originPrice: 60,
        isOriginPrice: false,
        rate: 6.6,
        discount: null,
        price: null,
        rating: 3.6,
        sales: 12,
        number: null,
        count: 0,
        hotSearch: true,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/1.jpg",
        title: "元气满分双人早餐",
        desc:
          "猪柳蛋麦满1份+原味板烧鸡腿麦满分1份+脆香油条1根+脆薯饼1只+鲜煮咖啡小1杯280ml+原味豆浆小1杯280ml",
        material: "主要原料：鸡肉,面包,风味酱,生菜条",
      },
      {
        id: 1,
        originPrice: 6.5,
        isOriginPrice: false,
        rate: 9,
        discount: null,
        price: null,
        rating: 3.3,
        sales: 32,
        number: null,
        count: 0,
        hotSearch: true,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/2.jpg",
        title: "脆薯饼",
        desc: "脆薯饼",
        material: "主要原料：薯饼",
      },
      {
        id: 2,
        originPrice: 32,
        isOriginPrice: false,
        rate: 6.8,
        discount: null,
        price: null,
        rating: 3,
        sales: 63,
        number: null,
        count: 0,
        hotSearch: false,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/3.jpg",
        title: "猪柳蛋满分三件套",
        desc: "猪柳蛋麦满分1个+脆薯饼1块+小原味豆浆1杯280ml",
        material: "主要原料：豆浆粉,水,薯饼,全麦松饼,猪柳饼,蒸蛋,切片干酪",
      },
      {
        id: 3,
        originPrice: 10,
        isOriginPrice: false,
        rate: null,
        discount: null,
        price: null,
        rating: 5,
        sales: 23,
        number: null,
        count: 0,
        hotSearch: true,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/4.jpg",
        title: "板烧满分三件套",
        desc: "板烧鸡腿麦满分1个+脆薯饼1块+小原味豆浆1杯280ml",
        material:
          "主要原料：豆浆粉,水,薯饼,721生菜条,长玉米面包,烤味酱,板烧鸡排",
      },
      {
        id: 4,
        originPrice: 21,
        isOriginPrice: false,
        rate: null,
        discount: null,
        price: null,
        rating: 4.5,
        sales: 21,
        number: null,
        count: 0,
        hotSearch: false,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/5.jpg",
        title: "菠萝鸡扒麦满分套餐",
        desc:
          "菠萝鸡扒麦满分1个+脆薯饼1个/脆香油条1个/玉米杯(大)1份+鲜煮咖啡(大)1杯/优品豆浆(大)1杯",
        material:
          "主要原料：菠萝鸡,扒麦满分,脆薯饼,脆香油条,玉米杯,鲜煮咖啡,优品豆浆",
      },
      {
        id: 5,
        originPrice: 19,
        isOriginPrice: false,
        rate: null,
        discount: null,
        price: null,
        rating: 4,
        sales: 26,
        number: null,
        count: 0,
        hotSearch: true,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/6.jpg",
        title: "皮蛋鸡肉粥精选套餐",
        desc: "皮蛋鸡肉粥1份+能量蛋1个+脆香油条1份",
        material: "主要原料：皮蛋,鸡肉,米，鸡蛋，油条",
      },
      {
        id: 6,
        originPrice: 26,
        isOriginPrice: false,
        rate: null,
        discount: null,
        price: null,
        rating: 3,
        sales: 99,
        number: null,
        count: 0,
        hotSearch: false,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/7.jpg",
        title: "麦乐送咸蛋粥专享套餐",
        desc: "原味板烧鸡腿麦满分1份+咸蛋黄鸡丝粥1碗+脆薯饼1块",
        material:
          "主要原料：大米，鸡肉，咸蛋黄，油，调味料，红芸豆，燕麦，薏米",
      },
      {
        id: 7,
        originPrice: 12,
        isOriginPrice: false,
        rate: null,
        discount: null,
        price: null,
        rating: 3.2,
        sales: 88,
        number: null,
        count: 0,
        hotSearch: false,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/8.jpg",
        title: "菠萝鸡扒麦满分",
        desc: "菠萝鸡扒麦满分1份",
        material: "主要原料：菠萝,鸡肉,英式松饼",
      },
      {
        id: 8,
        originPrice: 6.5,
        isOriginPrice: false,
        rate: null,
        discount: null,
        price: null,
        rating: 3,
        sales: 123,
        number: null,
        count: 0,
        hotSearch: true,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/9.jpg",
        title: "脆香油条",
        desc: "脆香油条",
        material: "",
      },
      {
        id: 9,
        originPrice: 7.5,
        isOriginPrice: false,
        rate: null,
        discount: null,
        price: null,
        rating: 2,
        sales: 36,
        number: null,
        count: 0,
        hotSearch: true,
        detail: false,
        imgUrl: "http://kxm.pub/shop/images/10.jpg",
        title: "香芋派",
        desc: "香芋派1份",
        material: "主要原料：小麦粉,芋头,水,白砂糖,起酥油",
      },
    ],
  },
  getters: {
    newProducts: (state) => {
      var newProducts = state.products.map((products) => {
        let price = Number(
          (
            products.originPrice * (products.rate / 10) || products.originPrice
          ).toFixed(1)
        );
        return {
          id: products.id,
          originPrice: products.originPrice,
          originPriceTotal: products.originPrice * products.number,
          isOriginPrice: products.isOriginPrice,
          rate: products.rate,
          price,
          discount: products.originPrice - products.price,
          rating: products.rating,
          sales: products.sales,
          number: products.number,
          count: products.count.toFixed(1),
          hotSearch: products.hotSearch,
          detail: products.detail,
          imgUrl: products.imgUrl,
          title: products.title,
          desc: products.desc,
          material: products.material,
        };
      });

      return newProducts.sort((a, b) => {
        let x = a[state.rst.sort];
        let y = b[state.rst.sort];
        if (state.rst.sort == "id") {
          return x < y ? -1 : x > y ? 1 : 0;
        } else {
          return x > y ? -1 : x < y ? 1 : 0;
        }
      });
    },
  },
  mutations: {
    openDetail(state, id) {
      state.products[id].detail = true;
      state.rst.currentDetailId = id;
    },
    closeDetail(state, id) {
      state.products[id].detail = false;
      state.rst.currentDetailId = null;
    },
    setCurrentDetailId(state, id) {
      state.rst.currentDetailId = id;
    },

    //添加商品
    addNumber(state, id) {
      let products = state.products[id];
      let accounts = state.accounts;
      let rst = state.rst;
      products.number++;

      // 有折扣的商品，每单只允许优惠一次
      if (rst.isOriginPrice && products.rate) {
        products.isOriginPrice = true;
        rst.isOriginPrice = false;
        rst.isPreferential = false;
        products.count = Number(
          (
            products.originPrice * (products.rate / 10) || products.originPrice
          ).toFixed(1)
        );
        accounts.tips = `已减${(products.originPrice - products.count).toFixed(
          1
        )}元`;
      }

      // 计算总额
      else {
        products.count += products.originPrice;
      }
    },

    //减少商品
    subNumber(state, id) {
      let products = state.products[id];
      let accounts = state.accounts;
      let rst = state.rst;
      if (products.number > 0) {
        // 预防快速点击number为负数
        products.number--;
        products.count -= products.originPrice;

        if (products.isOriginPrice && products.number == 0) {
          products.isOriginPrice = false;
          rst.isOriginPrice = true;
          rst.isPreferential = true;
          products.count = 0;
        } else if (products.rate && products.number == 0) {
          products.count = 0;
        }
      }
    },

    //清空
    RemoveCart(state) {
      if (state.accounts.number > 0) {
        state.rst.isOriginPrice = true;
        state.rst.isPreferential = true;
        (state.accounts.total = null),
          (state.accounts.count = null),
          (state.accounts.number = null),
          (state.accounts.preferential = null),
          (state.accounts.tips = null),
          state.products.forEach((item) => {
            item.isOriginPrice = false;
            item.number = null;
            item.count = 0;
          });
      }
    },

    dataCounts(state) {
      // 计算已加入购买车的产品数和总额
      let accounts = state.accounts;
      let rst = state.rst;
      accounts.number = 0;
      accounts.total = rst.foodBox;
      state.products.forEach((item) => {
        accounts.number += item.number;
        accounts.total += item.count;
        accounts.count = accounts.total;
      });

      // 如本单中没有折扣的商品将参与满减计算
      if (rst.isPreferential) {
        if (
          rst.preferential.length &&
          rst.preIndex + 1 < rst.preferential.length
        ) {
          if (accounts.count >= rst.preferential[rst.preIndex + 1].full) {
            rst.preIndex++;
          }
        }
        if (accounts.count >= rst.preferential[rst.preIndex].full) {
          accounts.total =
            accounts.count - rst.preferential[rst.preIndex].subtract; // 满减计算
          if (rst.preIndex + 1 < rst.preferential.length) {
            let price =
              rst.preferential[rst.preIndex + 1].full - accounts.count; //计算差价
            accounts.tips = `已减${
              rst.preferential[rst.preIndex].subtract
            }元,再买${price}元减${
              rst.preferential[rst.preIndex + 1].subtract
            }元`;
          } else {
            accounts.tips = `已减${rst.preferential[rst.preIndex].subtract}元`;
          }
        }
        if (accounts.count < rst.preferential[0].full) {
          let price = rst.preferential[rst.preIndex].full - accounts.count; //计算差价
          accounts.tips = `再买${price}元减${
            rst.preferential[rst.preIndex].subtract
          }元`;
        }
      }
    },
    sortItem(state, key) {
      state.rst.sort = key;
    },
  },
  actions: {},
});
